namespace SimpleWebServices.Web.UI.Html.Validation
{
    using System;
    using SimpleWebServices.Web.UI.Html.Elements;

    public sealed class StandardSelectValidator : Validator
    {
        public StandardSelectValidator()
            : this(null)
        {
        }

        public StandardSelectValidator(IInnerElements display)
            : base(display)
        {
        }

        public override bool Validate(IElement element)
        {
            if (null == element)
            {
                throw new ArgumentNullException("element");
            }

            return this.Validate(element as Select);
        }

        private bool Validate(Select control)
        {
            if (null == control)
            {
                throw new ArgumentOutOfRangeException("control");
            }

            this.Errors.Clear();

            if (!control.Contains(control.Value))
            {
                this.Errors.Add(new Error(this.ErrorText));
            }

            return 0 == this.Errors.Count;
        }
    }
}